import Vue from 'vue'
import Vuex from 'vuex'
import { ACCESS_TOKEN, USER_NAME, USER_INFO, WX_OPENID, WX_UNIONID } from "@/common/util/constants"
import { apiGetUserInfo, apiSaveDeviceInfo } from '@/api/user/user'

Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		token: '',
		userid: '',
		username: '',
		realname: '',
		nikename: '',
		avatar: '',
		usermobile: '',
		user: {},
        studentList:[], // 手机号下存在的学员列表
		examInfo:{
			topicOrder:"",
			sectionCount:0,
			duration:0,
		},
		exitSectionPrams:{
			
		},
		collectionName:"",
		duration:0,
		deviceInfo: {}
	},
	mutations: {
		SET_SOUND: (state, key) => {
			state.sound = key
		},
		SET_TOKEN: (state, token) => {
			state.token = token
		},
		SET_NAME: (state, {username, realname, nikename}) => {
			state.username = username
			state.realname = realname
			state.nikename = nikename
		},
		SET_MOBILE: (state, usermobile) => {
			state.usermobile = usermobile
		},
		SET_AVATAR: (state, avatar) => {
			state.avatar = avatar
		},
		SET_USERID: (state, userid) => {
			state.userid = userid
		},
		SET_USER: (state, userInfo) => {
			state.user = userInfo
		},
		changeExamInfo : (state,examInfo) => {
			console.log(examInfo);
			state.examInfo = examInfo;
			console.log('state.examInfo',state.examInfo);
		},
		changeExitSection :(state,prams) =>{
			state.exitSectionPrams = prams;
		},
		change_duration :(state,duration) =>{
			console.log('duration',duration);
			state.duration = duration;
		},
        SET_STUDEN_TLIST:(state,item)=>{
        	state.studentList=item
        },
		saveSystemInfo(state, data){
			state.deviceInfo = data
		}
	},
	actions: {
		// 保存设备信息
		saveSystemInfo({ commit }){
			const deviceInfo = wx.getDeviceInfo()
			const appBaseInfo = wx.getAppBaseInfo()
			console.log(deviceInfo, appBaseInfo, '设备信息')
			const { system, model, brand, language, version, platform } = {...deviceInfo, ...appBaseInfo}
			const params = { system, model, brand, language, version, platform }
			try {
				apiSaveDeviceInfo(params)
			} catch (error) {
				console.log(error, 'err')
			}
			commit('saveSystemInfo', params)
		},
		doLogin({ commit }, userInfo) {
            uni.setStorageSync(ACCESS_TOKEN, userInfo.token)
            uni.setStorageSync(WX_OPENID, userInfo.openId)
            uni.setStorageSync(WX_UNIONID, userInfo.unionId)
            
			commit('SET_TOKEN', userInfo.token)
		},
		reLogin({ commit }, userInfo) {
			uni.setStorageSync(ACCESS_TOKEN, userInfo.u2at)
			uni.setStorageSync(WX_OPENID, userInfo.openid)
			uni.setStorageSync(WX_UNIONID, userInfo.unionid)
			commit('SET_TOKEN', userInfo.u2at)
			commit('SET_USERID', userInfo.userid)
		},
		Logout({ commit, state }) {
			uni.clearStorageSync();
		},
		SetUserInfo({commit, state}, userInfo) {
			commit('SET_USER', userInfo)
			commit('SET_USERID', userInfo.userId)
			commit('SET_NAME', {username: '', realname: '', nikename: userInfo.nickName})
			commit('SET_MOBILE', userInfo.mobile)
		},
		GetUserInfo({commit}) {
			return new Promise((resolve, reject) => {
				apiGetUserInfo().then(res => {
					if(res.code == 200) {
						let user = res.data;
						console.log('user',user)
						uni.setStorageSync(USER_INFO, user)
						commit('SET_USER', user)
						commit('SET_USERID', user.userId)
						commit('SET_NAME', {username: '', realname: '', nikename: user.nickName})
						commit('SET_MOBILE', user.mobile)
						resolve(res)
					} else {
						reject(res)
					}
				}).catch(err => {
					reject(err)
				})
			})
		},
		changeExamInfoData({commit},examInfo){
			commit('changeExamInfo', examInfo)
			
		},
		changeExamDuration({commit},duration){
			commit('change_duration', duration)
		},
        setStudentList({commit}, list){
        	commit('SET_STUDEN_TLIST', list)
        },
	},
	getters: {
		token: state => state.token,
        studentList:state=>state.studentList,
		username: state => state.username,
		realname: state => state.realname,
		nikename: state => state.nikename,
		avatar: state => state.avatar,
		user: state => state.user
	}
})
